package br.inf.solutions.apresentacoes.ssp.view.composer;


import java.util.Arrays;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkplus.databind.BindingListModelListModel;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.ListModelArray;

import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.ReasonTransferReserveController;
import br.inf.solutions.apresentacoes.ssp.control.TransferReserveController;
import br.inf.solutions.apresentacoes.ssp.model.Person;
import br.inf.solutions.apresentacoes.ssp.model.ReasonTransferReserve;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.TransferReserve;
import br.inf.solutions.apresentacoes.ssp.model.enums.SalaryReserve;


@SuppressWarnings({ "serial" })
@org.springframework.stereotype.Component
@Scope("prototype")
public class TransferReserveComposer  extends BulletinItemComposer<TransferReserve, TransferReserveController> {

	private ReasonTransferReserve presentReason;

	public ReasonTransferReserve getPresentReason() {
		return presentReason;
	}

	public void setPresentReason(ReasonTransferReserve presentReason) {
		this.presentReason = presentReason;
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		loadBinder();
	}
	
	@Override
	protected String getUpdatePage() {
		return  "/paginas/transferenciareserva/modalCadastrar.zul";
	}

	@Override
	public TransferReserveController getControl() {
		return SpringFactory.getController("transferReserveController", TransferReserveController.class, ReflectionUtils.prepareDataForPersistence(this));
	}
	
	public List<SalaryReserve> getSalaryReserve() {
		return Arrays.asList(SalaryReserve.values());
	}

	@SuppressWarnings("unchecked")
	public List<ReasonTransferReserve> getReason(){
		ReasonTransferReserveController controlador = SpringFactory.getController("reasonTransferReserveController", ReasonTransferReserveController.class, null);
		List<ReasonTransferReserve> listParam = (List<ReasonTransferReserve>) controlador.doAction("listAllActivated").getList();
	
		return listParam;
	}
	@Override
	protected void initUpdatePage() {
		super.initUpdatePage();
		if(update)
			getReasonBySquarePost();
	}
	public void getReasonBySquarePost(){
		Return ret = getControl().doAction("getReasonBySquarePost");
		BindingListModelListModel<Object> list = new BindingListModelListModel<>(new ListModelArray<>(ret.getList()));
		
		
		((Combobox) getComponentById("fldReason")).setModel(list);
	}
	
	@SuppressWarnings("rawtypes")
	public EventListener getEventPersonNotFound(){
		return new EventListener<Event>() {

			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getName().equals("onOK")) {
					Executions.sendRedirect("/paginas/pessoas/cadastrar.zul");
				} else {
					getEntity().setPerson(new Person());
					loadBinder();
				}
				
			}
		};
	}
	
	public Return searchNotPublicatedTransferReserve(){
		Return ret = getControl().doAction("getNotPublicatedTransferReserve");
		if(ret.getList() != null && !ret.getList().isEmpty()){
			setEntity((TransferReserve) ret.getList().get(0));
			setEntity((TransferReserve) getEntity().cloneEntity());
		} else {
			Person person = getEntity().getPerson();
			setEntity(getEntityObject());
			getEntity().setPerson(person);
		}
		
		loadBinder();
		return ret;
	}

	
	@Override
	public TransferReserve getEntityObject() {
		return new TransferReserve();
	}

	@Override
	protected String getDeactivationMessage() {
		return null;
	}
	
	

}
